package com.tf_cut.admin.seller.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.annotation.Decrypt;
import com.tf_cut.admin.annotation.Encrypt;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.seller.vo.SellerAndAccountVo;
import com.tf_cut.admin.seller.vo.SellerOrderExcelVo;
import com.tf_cut.admin.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping("/seller/order/")
@Slf4j
public class SellerOrderController {
	
	@Autowired
	private BackEndApi sellerApi;
	
	// 进入商户订单清单页面
	@GetMapping("orderlist")
    public ModelAndView orderList() {
        return new ModelAndView("seller/order/orderlist", "order", null);
    }
	
	// 进入商户订单导入页面
	@GetMapping("orderimport")
	public ModelAndView orderImport() {
		return new ModelAndView("seller/order/orderimport", "order", null);
	}


    @PostMapping("/updateDetailPaymentStatus")
    public String updateDetailPaymentStatus(String json){
	    System.out.println(json);
        String res = sellerApi.updateDetailPaymentStatus(json);
        return null;
    }

	// 商户订单清单页面翻页
	@SuppressWarnings("unchecked")
	@PostMapping("orderlist/page")
	@Decrypt
    @Encrypt
    public Result<PageInfo<SellerAndAccountVo>> page(SellerAndAccountVo avo) {
	
        String json = JSON.toJSONString(avo);
        json = sellerApi.findListSellerOrder(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
	
	// 导入商户已完成订单
	@SuppressWarnings("unchecked")
	@RequestMapping("importorder")
	@ResponseBody
	public Result<Boolean> importOrder(@RequestParam("file") MultipartFile file) {
        List<SellerOrderExcelVo> importData = null;
        Result<Boolean> result = Result.of(true);
        try {
            importData = ExcelUtil.importExcel(file.getInputStream(), SellerOrderExcelVo.class);
        } catch (Exception e) {
        	// 导入异常
            log.error(e.getMessage(),e);
            result = Result.of(false);
            result.setMsg("导入失败！");
            result.setFlag(false);
            return result;
        }
        
        try {
        	String json = sellerApi.importSellerOrder(JSON.toJSONString(importData));
	        result = JSON.parseObject(json, Result.class);
        }catch(Exception e) {
        	// 导入异常
            log.error(e.getMessage(),e);
            result.setMsg("导入成功！返回页面有延迟，请勿重复导入。");
        }
        return result;
	}
	
}
